<template>
  <div style="width: 1200px;max-width: 1200px;margin: 0 auto;height: 100vh;">
    <span style="display: flex;justify-content: space-between;padding-top: 10px">
      <router-link to="/main/renshi/kaoqing/gwwaichu" style="">返回</router-link>
      <router-link to="/main/renshi/kaoqing/gwwaichu" style="">新增申请</router-link>
    </span>
    <h2>公务外出列表</h2>
    <div class="search-bar">
      <el-input v-model="searchForm.name" placeholder="姓名" style="width: 200px; margin-right: 10px;" />
      <el-input v-model="searchForm.department" placeholder="部门" style="width: 200px; margin-right: 10px;" />
      <el-button type="primary" @click="handleSearch">搜索</el-button>
    </div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="name" label="姓名" width="120"></el-table-column>
      <el-table-column prop="position" label="职务" width="120"></el-table-column>
      <el-table-column prop="department" label="部门" width="120"></el-table-column>
      <el-table-column prop="reason" label="外出事由" width="200"></el-table-column>
      <el-table-column prop="location" label="外出地点" width="120"></el-table-column>
      <el-table-column prop="managerApproval" label="部门经理审核" width="120"></el-table-column>
      <el-table-column prop="managementRecord" label="企业管理部记录" width="160"></el-table-column>
      <el-table-column label="操作" width="230" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleDetail(scope.row)">详情</el-button>
          <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <!-- 详情对话框 -->
    <el-dialog title="公务外出详情" :visible.sync="dialogVisible" width="50%">
      <div v-if="detailData">
        <p><strong>姓名：</strong>{{ detailData.name }}</p>
        <p><strong>职务：</strong>{{ detailData.position }}</p>
        <p><strong>部门：</strong>{{ detailData.department }}</p>
        <p><strong>外出事由：</strong>{{ detailData.reason }}</p>
        <p><strong>外出地点：</strong>{{ detailData.location }}</p>
        <p><strong>部门经理审核：</strong>{{ detailData.managerApproval }}</p>
        <p><strong>企业管理部记录：</strong>{{ detailData.managementRecord }}</p>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getBusinessTripList, getBusinessTripDetail, deleteBusinessTrip } from '@/api/businessTrip';

export default {
  data() {
    return {
      searchForm: {
        name: '',
        department: ''
      },
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      dialogVisible: false,
      detailData: null
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    async fetchData() {
      try {
        const response = await getBusinessTripList({
          current: this.currentPage,
          size: this.pageSize,
          name: this.searchForm.name,
          department: this.searchForm.department
        });
        this.tableData = response.records;
        this.total = response.total;
      } catch (error) {
        this.$message.error('获取数据失败：' + error.message);
      }
    },
    handleSearch() {
      this.currentPage = 1;
      this.fetchData();
    },
    handleEdit(row) {
      this.$router.push(`/main/renshi/kaoqing/gwwaichu?id=${row.id}&type=edit`);
    },
    async handleDetail(row) {
      try {
        const response = await getBusinessTripDetail(row.id);
        this.detailData = response.data;
        this.dialogVisible = true;
      } catch (error) {
        this.$message.error('获取详情失败：' + error.message);
      }
    },
    async handleDelete(row) {
      try {
        await this.$confirm('确认删除该公务外出记录？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        });
        await deleteBusinessTrip(row.id);
        this.$message.success('删除成功');
        this.fetchData();
      } catch (error) {
        if (error !== 'cancel') {
          this.$message.error('删除失败：' + error.message);
        }
      }
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchData();
    }
  }
};
</script>

<style scoped>
.el-table {
  margin-top: 20px;
}

.el-pagination {
  margin-top: 20px;
  text-align: right;
}

.search-bar {
  margin-bottom: 20px;
}
</style>